package com.example.cet;

import java.security.PublicKey;

import loginCET.*;

import android.R.bool;
import android.R.integer;
import android.R.string;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.style.SuperscriptSpan;
import android.text.style.UpdateAppearance;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.content.*;

public class MainActivity extends Activity {
	private EditText nameEditText = null;
	private EditText numberEditText = null;
	private Button loginbButton = null;
	private ProgressDialog pDialog = null;
	private Login login = Login.getLogin();


	// 对异常的处理
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO 自动生成的方法存根
			super.handleMessage(msg);
			switch (msg.what) {
			case 0:
				pDialog.dismiss();
				Toast.makeText(getApplicationContext(), "获取成功",
						Toast.LENGTH_SHORT).show();
				Intent intent=new Intent();
				intent.setClass(MainActivity.this, DisplayActivity.class);
				MainActivity.this.startActivity(intent);
				break;
			case 1:
				Toast.makeText(getApplicationContext(), "获取失败，姓名或者考号输入错误",
						Toast.LENGTH_LONG).show();
				pDialog.dismiss();
				nameEditText.setText("");
				numberEditText.setText("");
				break;
			case 2:
				Toast.makeText(getApplicationContext(), "连接网络超时，请稍后再试",
						Toast.LENGTH_LONG).show();
				pDialog.dismiss();
				break;
			default:
				Toast.makeText(getApplicationContext(), "请重新登录...",
						Toast.LENGTH_LONG).show();
				pDialog.dismiss();
				nameEditText.setText("");
				numberEditText.setText("");
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		nameEditText = (EditText) findViewById(R.id.CET_usename);
		numberEditText = (EditText) findViewById(R.id.CET_number);
		loginbButton = (Button) findViewById(R.id.scorenext);

		loginbButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				if (check()) {

					// 判断联网
					if (isConnectInternet(getApplicationContext())) {
						pDialog = ProgressDialog.show(MainActivity.this, "",
								"正在查询 ....", false, false);

						new Thread(new Runnable() {

							@Override
							public void run() {
								// TODO 自动生成的方法存根
								int myException = login.loginChsi(nameEditText
										.getText().toString(), numberEditText
										.getText().toString());

								if (myException == 0) {
									// 网络故障
									handler.sendEmptyMessage(2);
								} else if (myException == 2) {
									// 名字或者考号输入错误
									handler.sendEmptyMessage(1);
								} else {
									// 成功登陆
									handler.sendEmptyMessage(0);
								}
							}
						}).start();
					}
				}

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	// 检查一下输入信息
	public boolean check() {
		if (nameEditText.getText().toString().equals("")) {
			Toast.makeText(getApplicationContext(), "请输入姓名", 1000);
			return false;
		} else if (numberEditText.getText().toString().equals("")) {
			Toast.makeText(getApplicationContext(), "请输入考号", 1000);
			return false;
		} else {
			return true;
		}
	}

	// 判断是否联网
	public boolean isConnectInternet(Context c) {
		ConnectivityManager conManager = (ConnectivityManager) c
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
		if (networkInfo == null || !networkInfo.isConnected()) {
			return false;
		}
		if (networkInfo.isConnected()) {
			/******************************************
			 * if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
			 * System.out.println("网络类型：WIFI"); else if (networkInfo.getType()
			 * == ConnectivityManager.TYPE_MOBILE)
			 * System.out.println("网络类型：手机"); else
			 * System.out.println("网络类型：未知");
			 ****************************************/
			return true;
		}
		return false;
	}

	//更新activity的函数
//	private void Update() {
//		ContentValues temp = Login.getContentValues();
//		nametTextView.setText(temp.get("name").toString());
//		schooltTextView.setText(temp.get("school").toString());
//		typeOFCETtTextView.setText(temp.get("type").toString());
//		idtTextView.setText(idtTextView.getText().toString());
//		numTextView.setText(temp.get("sum").toString());
//		listen_scoreTextView.setText(temp.get("listen").toString());
//		write_scoretTextView.setText(temp.get("write").toString());
//		mixed_scoreTextView.setText(temp.get("synthesize").toString());
//		read_scoreTextView.setText(temp.get("read").toString());
//	}

}
